package com.yunjing.elasticjob.elastic;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhiqi.shao
 * @Date 2018/6/15 9:13
 */
@Slf4j
public class MyElasticJob implements SimpleJob {

    /**
     * 执行分片任务
     * 测试分两片：数据id%分片个数==当前分片就执行任务的刷新
     * 1. id=1,id=2,id=3,id=4
     * 2. 1%2=1:分片1执行，2%2=0:分片0执行，3%2=1:分片1执行，4%2=0：分片0执行
     *
     * @param context 分片信息上下文
     */
    @Override
    public void execute(ShardingContext context) {
        String jobConfigId=context.getJobParameter();
        if(Integer.parseInt(jobConfigId)%context.getShardingTotalCount()==context.getShardingItem()){
            //打印出任务相关信息，JobParameter用于传递任务的IDlog.info("任务名：{}, 片数：{}, id={}", context.getJobName(), context.getShardingTotalCount(),
            log.info("ElasticJob is run ...............jobName:{},sharding:{},taskId:{},jobParms:{}",context.getJobName(),context.getShardingItem(),context.getTaskId(),context.getJobParameter());
            context.getJobParameter();
        }

    }
}
